<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    SessionList:[] as Array<any>,
  })
  interface formQuert {
    id:number
    game_id:number
    content:string
    circulate:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    content: [
      { required: true, message: '请填写描述', trigger: 'change' }
    ],
    circulate: [
      { required: true, message: '请填写循环次数', trigger: 'change' }
    ],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const { id=0 } = form
      let submitSet = false
      if(id) {
        await ChallengeApi.whiteEdit(form).then(res=>{
          submitSet = true
          ToastSuccess('修改成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await ChallengeApi.WhiteAdd(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      id=0,
      game_id=1,
      content='',
      circulate='',
      SessionList=[]
    } = query
    let formQuery = {
      game_id,
      content,
      circulate,
    } as formQuert
    if(id) formQuery['id'] = id
    Object.assign(form,formQuery)
    action.SessionList = SessionList
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="`${form.id?'修改':'新增'}白名单`">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <template v-if="!form.id">
          <el-form-item :label-width="formLabelWidth" label="游戏类型：">
            <el-select v-model="form.game_id" placeholder="请选择">
              <el-option
                v-for="item in action.SessionList"
                :key="item.game_id"
                :label="item.game_name"
                :value="item.game_id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>
        <el-form-item :label-width="formLabelWidth" label="循环次数" prop="circulate">
          <el-input v-model="form.circulate" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="描述" prop="content">
          <el-input type="textarea" v-model="form.content" :autosize="{ minRows: 4}" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>